/*
 * DatagramBuilder Library: serialize/deserialize data to/from datagram
 * Copyright (C) 2008 Rakov Ilya <RakovIlya@gmail.com>
 *
 * This library can be distributed under the terms of the GNU LGPLv2.
 * See the file COPYING.LIB.
 */


#ifndef DATAGRAM_LISTPARSER_H_
#define DATAGRAM_LISTPARSER_H_


/**
 * @file ListParser.h
 *
 * @brief ListParser header file
 *
 * @date Dec 15, 2008
 * @author Rakov Ilya
 */


#include "Parser.h"
#include "Mark.h"
#include "Tag.h"


/**
 * @brief This template class provides an ability to deserialize
 * lists of the values.
 */
template< class ElemT, class CountT >
class ListParser
{

public: // user interface

  /**
   * @brief ctor.
   */
  ListParser( Parser &parser );


  /**
   * @return Count of elements in the list.
   */
  inline CountT GetCount() const;


  /**
   * @return Flag whether we have treated list.
   */
  inline bool IsTreated() const;


public: /**@{ @name Deserialization operators */

  /**
   * @brief Deserializes element from the list
   *
   * @param value Data to deserialize
   * @exception deserialization_error
   * @note Provides exception-safety.
   */
  inline ListParser& operator >>( ElemT &value );


  /** @relates Parser
   *
   * @brief Common way to check data consistency.
   *
   * Result of the data deserialization compares with data sample value.
   * On equality nothing happens or else the deserialization_error throws.
   *
   * @param value The data sample
   * @exception deserialization_error
   * @note Provides exception-safety.
   */
  inline ListParser& operator >>( const ElemT &value );

/**@}*/


private: // implementation

  Parser         &parser_;
  Mark< CountT >   count_;
  CountT         treated_;

};


/*
 * @brief This template class provides an ability to deserialize
 * tagged lists of the values.
 */
template< class ElemT, class CountT >
class ListParser< Tag<ElemT>, CountT >
{

public: // user interface

  /**
   * @brief ctor.
   */
  ListParser( Parser &parser );


  /**
   * @return Count of elements in the list.
   */
  inline CountT GetCount() const;


  /**
   * @return Flag whether we have treated list.
   */
  inline bool IsTreated() const;


public: /**@{ @name Deserialization operators */

  /**
   * @brief Deserializes element from the list
   *
   * @param value Data to deserialize
   * @exception deserialization_error
   * @note Provides exception-safety.
   */
  inline ListParser& operator >>( ElemT &value );


  /** @relates Parser
   *
   * @brief Common way to check data consistency.
   *
   * Result of the data deserialization compares with data sample value.
   * On equality nothing happens or else the deserialization_error throws.
   *
   * @param value The data sample
   * @exception deserialization_error
   * @note Provides exception-safety.
   */
  inline ListParser& operator >>( const ElemT &value );

/**@}*/


private: // implementation

  ListParser< ElemT, CountT > listParser_;

};


#include "impl/ListParserImpl.h"


#endif /* DATAGRAM_LISTPARSER_H_ */
